package com.quanquan.management.service;

import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.SquareDTO;
import com.quanquan.management.request.SquareRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;

import java.util.List;

/**
 * @ClassName:SquareService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/12 9:57
 * @Version 1.0
 */
@Service
public class SquareService {
    @Value("${module-url.storeUrl}")
    private String storeUrl;


    /**
     * 新增商城兑换数量限制
     *
     * @param request
     * @return
     */
    public boolean addCount(SquareRequest request) {
        if (null == request) {
            return false;
        }
        String json = Request.postJson(storeUrl + "square/add_count", request);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            String data = jsonObject.getString("data");
            if (null != data) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查看购买限制次数
     *
     * @return
     */
    public SquareDTO getById(String id) {
        SquareRequest squareRequest = new SquareRequest();
        squareRequest.setId(id);
        String json = Request.postJson(storeUrl + "square/find_by_id", squareRequest);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        SquareDTO squareDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONObject("data").toJavaObject(SquareDTO.class);
        }
        return squareDTO;
    }

    public List<SquareDTO> list() {
        SquareRequest squareRequest = new SquareRequest();
        String postJson = Request.postJson(storeUrl + "square/list", squareRequest);
        JSONObject jsonObject = JSONObject.parseObject(postJson);
        Integer resCode = jsonObject.getInteger("resCode");
        List<SquareDTO> pageDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONArray("data").toJavaList(SquareDTO.class);
        }
        return pageDTO;
    }

}
